/**
 * Copyright 2013 Matt Holland <code.monkey.ota at gmail.com>
 *
 * This file is part of BotTender.
 *
 * BotTender is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * BotTender is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with BotTender. If not, see <http://www.gnu.org/licenses/>.
 */

package ota.ircbot.config;

import java.io.*;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

// Stores the configuration for the bot, this includes: Server, Port, Nickname, etc
// to start, but the class allows any object to be stored in it. Making it easy to expand.


public class BotConfig implements Serializable {

	private static final long serialVersionUID = 7153432450423890303L;
	
	protected Map<String, Object> options = new HashMap<String, Object>();
	
	// TODO: Check to make sure each type passed can be serialized, else throw and exception
	public void setOption(String key, Object val) {
		options.put(key, val);
		save();
	}//End setOption
	
	// This function requires the class be specified, if it does not match the object in the map, throw a ParseException
	public Object getOption(String key, Class<?> cls) throws ParseException { // EX: String.class, String[].class, Integer.class, etc.
		if(options.containsKey(key)) {
			if(cls.isInstance(options.get(key))) {
				return options.get(key);
			}// End nested if
			else {
				throw new ParseException("Valid Key, Invalid Object Type.", 42);
			}
		} // End outer if
		return null;
	} // End getOptions
	
	public Boolean exists(String key) {
		return options.containsKey(key); // Simple wrapper around containsKey since the map is Protected
	}// End exists
	
	//TODO: Implement the following functions: Load.
	public Boolean save() {
		try {
			FileOutputStream fileOut = new FileOutputStream("config.ser");
			ObjectOutputStream oOut = new ObjectOutputStream(fileOut);
			oOut.writeObject(this);
			oOut.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	} // End Save
	public static Object load() {
		try {
			FileInputStream fileIn = new FileInputStream("config.ser");
			ObjectInputStream oIn = new ObjectInputStream(fileIn);
			Object res = oIn.readObject();
			oIn.close();
			return res;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}// End BotConfig
